package com.itboye.ihomebank.activity.key;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.ServerError;
import com.android.volley.TimeoutError;
import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.base.MyApplcation;
import com.itboye.ihomebank.bean.KaiSuoJiLu;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.bean.SIDBean;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.interfaces.IRefreshAfterAlert;
import com.itboye.ihomebank.presenter.KeyPresenter;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

/**
 * 钥匙权限管理
 */
public class ActivityKaiSuoJiLu extends BaseOtherActivity implements Observer {
	View v_statusbar;
	ImageView key_title_finish;
	TextView key_title_title;
	RelativeLayout key_title_xuanze;
	TextView key_title_xuan;
	TextView shouquan, dongjie, delete;
	ImageView key_title_shezhi;//下拉的那个框

	private String lockId;//锁id
	private String keyId;//钥匙id
	private String toUid;//钥匙的用户id
	private String status;//锁状态
	private String type;//1代表授权
	private String start;//开始时间
	private String end;//结束时间
	private String uid;
	private int cz;//记录数据请求操作

	private List<KaiSuoJiLu.KaiSuoJiLuList> beans=new ArrayList<>();
	KeyPresenter keyPresenter;

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.key_title_finish:
			finish();
			break;
		case R.id.key_title_xuan:
		case R.id.key_title_xuanze:
			Intent intent = new Intent(ActivityKaiSuoJiLu.this,UpdateKeyTimeActivity.class);
			intent.putExtra("lockid", lockId);
			intent.putExtra("keyid", keyId);
			intent.putExtra("start", start);
			intent.putExtra("end", end);
			startActivity(intent);
			break;
		case R.id.shouquan:// 授权
			cz=1;
			String sid=SPUtils.get(MyApplcation.ctx,null,SPContants.LOCK_SESSION,"")+"";
			showProgressDialog("修改中,请稍后......", true);
			if(shouquan.getText().toString().equals("授权")){
				if ("".equals(sid)){
					keyPresenter.getShouQuan(uid, toUid, lockId, "1","0","0");
				}else {
					keyPresenter.getShouQuan(sid, toUid, lockId, "1","0","0");
				}

			}else{
				if ("".equals(sid)){
					keyPresenter.getShouQuan(uid, toUid, lockId, "0","0","0");
				}else {
					keyPresenter.getShouQuan(sid, toUid, lockId, "0","0","0");
				}

			}

			break;
		case R.id.dongjie:// 冻结，解冻
			cz=2;
			String sid1=SPUtils.get(MyApplcation.ctx,null,SPContants.LOCK_SESSION,"")+"";
			showProgressDialog("修改中,请稍后......", true);
			if(dongjie.getText().toString().equals("冻结")){
				if ("".equals(sid1)){
					keyPresenter.getDongJie(uid, toUid, lockId, keyId);
				}else {
					keyPresenter.getDongJie(sid1, toUid, lockId, keyId);
				}

			}else{
				if ("".equals(sid1)){
					keyPresenter.getJieDong(uid, toUid, lockId, keyId);
				}else {
					keyPresenter.getJieDong(sid1, toUid, lockId, keyId);
				}

			}
			
			break;
		case R.id.delete:// 删除
			cz=3;
			String sid2=SPUtils.get(MyApplcation.ctx,null,SPContants.LOCK_SESSION,"")+"";
			showProgressDialog("修改中,请稍后......", true);
			if ("".equals(sid2)){
				keyPresenter.getShanChu(uid, toUid, lockId, keyId);
			}else {
				keyPresenter.getShanChu(sid2, toUid, lockId, keyId);
			}

			break;
		default:
			break;
		}

	}

	@Override
	protected int layout_id() {
		return R.layout.activity_kaisuo_jilu;
	}

	@Override
	protected void onInitController() {

	}

	@Override
	protected void onInit() {
		setStatusBarHeight(v_statusbar);
		key_title_title.setText("钥匙权限管理");
		key_title_xuan.setText("修改");
		key_title_shezhi.setVisibility(View.GONE);
		key_title_xuan.setVisibility(View.VISIBLE);
		keyPresenter=new KeyPresenter(this);
		lockId = getIntent().getStringExtra("lockid");
		keyId = getIntent().getStringExtra("keyid");
		toUid = getIntent().getStringExtra("touid");
		status = getIntent().getStringExtra("status");
		start= getIntent().getStringExtra("start");
		end= getIntent().getStringExtra("end");
		if (status.equals("110405")) {
			dongjie.setText("解冻");
		} else {
			dongjie.setText("冻结");
		}
		type = getIntent().getStringExtra("user_type");

		if (type.equals("1")) {
			shouquan.setText("取消授权");
		} else {
			shouquan.setText("授权");
		}

		uid = SPUtils.get(this, null, SPContants.USER_ID, "") + "";

		IntentFilter intentFilter=new IntentFilter(UpdateKeyTimeActivity.UPDATE_KEY);
		registerReceiver(receiver,intentFilter);

	}
	BroadcastReceiver receiver=new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			finish();
		}
	};

	@Override
	protected void onDestroy() {
		super.onDestroy();
		unregisterReceiver(receiver);
	}

	@Override
	protected void onReadIntent(Bundle bundle) {

	}

	@Override
	public void update(Observable observable, Object data) {
		ResultEntity entity = handlerError(data);
		try {
			closeProgressDialog();
		} catch (Exception e) {
		}
		if (entity != null) {
			//冻结
			if(entity.getEventType() == KeyPresenter.dongjie_success){
				ByAlert.alert(entity.getData());
				dongjie.setText("解冻");
			}else
			if(entity.getEventType() == KeyPresenter.dongjie_fail){
				ByAlert.alert(entity.getData());
			}else
			//解冻
			if(entity.getEventType() == KeyPresenter.jiedong_success){
				ByAlert.alert(entity.getData());
				dongjie.setText("冻结");
			}else
			if(entity.getEventType() == KeyPresenter.jiedong_fail){
				ByAlert.alert(entity.getData());
			}else
			//授权
			if(entity.getEventType() == KeyPresenter.shouquan_success){
				ByAlert.alert(entity.getData());
				shouquan.setText("取消授权");
			}else
			if(entity.getEventType() == KeyPresenter.shouquan_fail){
				ByAlert.alert(entity.getData());
			}
			//取消授权
			if(entity.getEventType() == KeyPresenter.shouquan_quxiao_success){
				ByAlert.alert(entity.getData());
				shouquan.setText("授权");
			}else
			if(entity.getEventType() == KeyPresenter.shouquan_quxiao_fail){
				ByAlert.alert(entity.getData());
			}else
			//删除
			if(entity.getEventType() == KeyPresenter.shanchu_success){
				ByAlert.alert(entity.getData());
			}else
			if(entity.getEventType() == KeyPresenter.shanchu_fail){
				ByAlert.alert(entity.getData());
			} else if (entity.getEventType()== UserPresenter.By_GETSID_success){//获取sid 成功
				SIDBean data1 = (SIDBean) entity.getData();
				SPUtils.put(MyApplcation.ctx,null, SPContants.LOCK_SESSION,data1.getSid());
				SPUtils.put(MyApplcation.ctx,null,SPContants.ISLOCK,true);
				toRefreshData();
			}else if (entity.getEventType()==UserPresenter.By_GETSID_fail){
				ByAlert.alert(entity.getMsg());
			}

			//服务器请求失败 切换服务器
			if (entity.getData() instanceof ServerError ||entity.getData() instanceof TimeoutError){//服务器失败
				//切换 锁备用服务器
				showAlert();
			}
		}
	}


	private void toRefreshData() {
		String sid=SPUtils.get(MyApplcation.ctx,null,SPContants.LOCK_SESSION,"")+"";
		switch (cz){
			case 1:
				if (shouquan.getText().toString().equals("授权")){
				if ("".equals(sid)){
					keyPresenter.getShouQuan(uid, toUid, lockId, "1","0","0");
				}else {
					keyPresenter.getShouQuan(sid, toUid, lockId, "1","0","0");
				}

			}else{
				if ("".equals(sid)){
					keyPresenter.getShouQuan(uid, toUid, lockId, "0","0","0");
				}else {
					keyPresenter.getShouQuan(sid, toUid, lockId, "0","0","0");
				}

			}
				break;
			case 2:
				if(dongjie.getText().toString().equals("冻结")){
					if ("".equals(sid)){
						keyPresenter.getDongJie(uid, toUid, lockId, keyId);
					}else {
						keyPresenter.getDongJie(sid, toUid, lockId, keyId);
					}

				}else{
					if ("".equals(sid)){
						keyPresenter.getJieDong(uid, toUid, lockId, keyId);
					}else {
						keyPresenter.getJieDong(sid, toUid, lockId, keyId);
					}

				}
				break;
			case 3:
				if ("".equals(sid)){
					keyPresenter.getShanChu(uid, toUid, lockId, keyId);
				}else {
					keyPresenter.getShanChu(sid, toUid, lockId, keyId);
				}
				break;
		}
	}
}
